package net.learn.system.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.AllArgsConstructor;
import net.learn.framework.common.page.PageResult;
import net.learn.framework.common.service.impl.BaseServiceImpl;
import net.learn.system.convert.SysNewConvert;
import net.learn.system.dao.SysNewDao;
import net.learn.system.entity.SysNewEntity;
import net.learn.system.query.SysNewQuery;
import net.learn.system.service.SysNewService;
import net.learn.system.utils.UUIDUtil;
import net.learn.system.vo.SysNewVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 新闻管理
 */
@Service
@AllArgsConstructor
public class SysNewServiceImpl extends BaseServiceImpl<SysNewDao, SysNewEntity> implements SysNewService {

    @Override
    public PageResult<SysNewVO> page(SysNewQuery query) {
        IPage<SysNewEntity> page = baseMapper.selectPage(getPage(query), getWrapper(query));

        return new PageResult<>(SysNewConvert.INSTANCE.convertList(page.getRecords()), page.getTotal());
    }

    @Override
    public List<SysNewVO> getList(SysNewQuery query) {
        List<SysNewEntity> entityList = baseMapper.selectList(getWrapper(query));

        return SysNewConvert.INSTANCE.convertList(entityList);
    }

    private Wrapper<SysNewEntity> getWrapper(SysNewQuery query){
        LambdaQueryWrapper<SysNewEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StrUtil.isNotBlank(query.getTitle()), SysNewEntity::getTitle, query.getTitle());
        return wrapper;
    }

    @Override
    public void save(SysNewVO vo) {
        SysNewEntity entity = SysNewConvert.INSTANCE.convert(vo);
        entity.setUid(UUIDUtil.uuid());
        baseMapper.insert(entity);
    }

    @Override
    public void update(SysNewVO vo) {
        SysNewEntity entity = SysNewConvert.INSTANCE.convert(vo);

        updateById(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(List<Long> idList) {
        // 删除岗位
        removeByIds(idList);
    }

}